<?php
namespace app\index\controller;

use app\common\controller\Frontend;
//SOAP Server
//non-WSDL 模式
class Modulestock extends Frontend
{
    protected $noNeedLogin = ['*'];

    //http://ela.test/index/modulestock/info?moduleid=kfj
    public function info($moduleid = ''){

        $iteminfo = array(
            'moduleid' => $moduleid,
            'adf' => "dfdddeew"
        );
        return $moduleid."---sf2111";
    }


    //http://ela.test/index/home/index
    //http://ela.test/index/modulestock/info2?moduleid=kfj
    public function info2($moduleid = '')
    {
        return "-vasd12---".$moduleid;
    }

    public function returninfo2($xml = '') {
        return $xml."---returninfo2";
    }

    //ela
    public function returninfo($xml = '')
    {
        $elalog2 = new \app\admin\model\ela\Log2();
        $elalog2->content = $xml;
        $elalog2->save();

        $data = $this->xmlToArray($xml);
        $this->doEla($data);
        $r = "<eWMSMessage><ReportData><Result>Y</Result><ResultMessage></ResultMessage></ReportData></eWMSMessage>";
        return $r;
    }

    function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }

    function doEla($data)
    {
        if (!isset($data['EventID'])) {
            return;
        }

        if ($data['EventID'] == 'BuyData') {
            //進貨單回報
            foreach ($data['BuyDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'SellData') {
            //銷貨單回報
            foreach ($data['SellDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'ProduceInData') {
            //生產入庫單回報
            foreach ($data['ProduceInDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'PickUpData') {
            //領料單回報
            foreach ($data['PickUpDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'ReturnData') {
            //退料單回報
            foreach ($data['ReturnDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'SellBackData') {
            //銷退單回報
            foreach ($data['SellBackDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'TransferData') {
            //調撥單回報
            foreach ($data['TransferDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'BuyReturnData') {
            //退貨單回報
            foreach ($data['BuyReturnDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'InventoryData') {
            //盤點單回報
            foreach ($data['InventoryDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else {
            //單據狀態回報格式
            foreach ($data['Data']['FormHead']['RecordList'] as $row) {
                if (isset($row['MO001'])) {

                } else if (isset($row['PU001'])) {
                    //進貨單單別
                    if (intval($row['PU200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['PU001'])->where('receipt_number', $row['PU002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }
                } else if (isset($row['CO001'])) {
                    //銷貨單單別
                    if (intval($row['CO200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['CO001'])->where('receipt_number', $row['CO002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['MP001'])) {
                    //生產入庫單單別
                    if (intval($row['MP200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MP001'])->where('receipt_number', $row['MP002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }
                } else if (isset($row['MU001'])) {
                    //領料單單別
                    if (intval($row['MU200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MU001'])->where('receipt_number', $row['MU002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['MR001'])) {
                    //退料單單別
                    if (intval($row['MR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MR001'])->where('receipt_number', $row['MR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['CR001'])) {
                    //銷退單單別
                    if (intval($row['CR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['CR001'])->where('receipt_number', $row['CR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['IN001'])) {
                    //調撥單單別
                    if (intval($row['IN200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['IN001'])->where('receipt_number', $row['IN002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['PR001'])) {
                    //退貨單單別
                    if (intval($row['PR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['PR001'])->where('receipt_number', $row['PR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['IS001'])) {
                    //盤點單單別
                    if (intval($row['IS200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['IS001'])->where('receipt_number', $row['IS002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                }
            }

        }


    }

}

//$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : (isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');

$server = new \SoapServer(null,array('uri' => "abcd"));
$server->setClass("\app\index\controller\Modulestock");
//$server->service($HTTP_RAW_POST_DATA);
$server->handle();



